<template>
  <div class="login-container">
    <van-nav-bar
      title="登录"
      class="page-nav-bar"
    >
      <van-icon name="cross" slot="left" @click="$router.back()"/>
    </van-nav-bar>
    <!-- 获取到表单用validate进行单独验证 -->
    <van-form @submit="onSubmit" validate-first ref="loginForm">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="userFormRules.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        type="number"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
        maxlength="6"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down :time="time" v-if="isCountDownShow" @finish="isCountDownShow = false" format="ss s"/>
          <!-- native-type防止按钮被点击时触发提交事件 -->
          <van-button v-else size="small" class="send-sms-btn" type="primary" native-type="button" @click="sendLoginMsm">发送验证码</van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button block type="info" class="login-btn" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
// 导入登录接口
import { login, sendSms } from '@/api/user'
export default {
  data () {
    return {
      username: '',
      password: '',
      user: {
        mobile: '',
        code: ''
      },
      // 表单校验规则
      userFormRules: {
        mobile: [{
          required: true,
          message: '手机号不能为空'
        },
        {
          pattern: /^1[1|3|5|7|8]\d{9}$/,
          message: '手机号码格式不正确'
        }],
        code: [{
          required: true,
          message: '验证码不能为空'
        },
        {
          pattern: /^\d{6}$/,
          message: '验证码格式不正确'
        }]
      },
      time: 1000 * 60,
      isCountDownShow: false
    }
  },
  methods: {
    async onSubmit () {
      // 获取表单数据
      const user = this.user
      // 请求前进行加载状态的提示
      // toast记得首字母小写
      this.$toast.loading({
        message: '登录中...',
        forbidClick: false, // 禁用背景点击
        duration: 0
      })
      try {
        const { data } = await login(user)
        this.$toast.success('登录成功')
        this.$store.commit('setUser', data.data)
        this.$router.push('/my')
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('手机格式或验证码错误')
        } else {
          this.$toast.fail('登录失败,请稍后再试')
        }
      }
    },
    // 点击发送验证码
    async sendLoginMsm () {
      // 首先校验号码
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return console.log('验证失败,' + err.message)
      }
      this.isCountDownShow = true
      try {
        const res = await sendSms(this.user.mobile)
        this.$toast('发送成功' + res)
      } catch (err) {
        if (err.response.status === 429) {
          this.$toast('请勿频繁发送验证码')
        } else {
          this.$toast('发送失败，请稍后再试')
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  .toutiao {
    font-size: 37px;
  }

  .send-sms-btn {
    height: 46px;
    border: none;
    border-radius: 23px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }

  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
